#include <vector>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=90 lang=cpp
 *
 * [90] 子集 II
 */

// @lc code=start
class Solution {
public:
    
    vector<vector<int>> res;

    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<int> temp;
        dfs(temp, 0, nums);
        return res;
    }

    void dfs(vector<int> & now, int index, vector<int> & nums) {
        res.push_back(now);
        if (index == nums.size()) return;
        int last;
        for (int i = index; i < nums.size(); i++) {
            if (i == index || last != nums[i]) {
                last = nums[i];
                now.push_back(nums[i]);
                dfs(now, i + 1, nums);
                now.pop_back();
            }
        }
    }
};
// @lc code=end

